import 'package:flutter/material.dart';

// https://dart.dev/guides/language/language-tour#functions

class FunctionPage extends StatefulWidget {
  final Map<String, dynamic> arguments;
  FunctionPage({Key key, this.arguments}) : super(key: key);

  @override
  _FunctionPageState createState() => _FunctionPageState();
}

class _FunctionPageState extends State<FunctionPage> {
  @override
  Widget build(BuildContext context) {
    return Container(
      child: Scaffold(
        appBar: AppBar(
          title: Text('FunctionPage'),
        ),
        body: ListView(
          children: <Widget>[
            ListTile(
              title: Text('内置函数'),
              onTap: () {
                print(''); // 内置方法
              },
            ),
            Divider(height: 1.0),
            ListTile(
              title: Text('自定义函数<参数 返回值>'),
              onTap: () {
                Function customFunc(Function doing) {
                  print('call func');
                  doing();
                  return () {
                    return 'copy that.';
                  };
                }

                void doing() {
                  print('doing');
                }

                final inform = customFunc(doing);
                final msg = inform();
                print(msg);
              },
            ),
            Divider(height: 1.0),
            ListTile(
              title: Text('可选参数 <optional parameters> <默认值>'),
              onTap: () {
                void optionArgument(String name, [int age, String sex = 'maintain secrecy']) {
                  if (age != null) {
                    print('可选 age 有值');
                  } else {
                    print('可选 age 无值');
                  }
                }

                optionArgument('Shawn Mendes');
                optionArgument('Shawn Mendes', 18);
              },
            ),
            Divider(height: 1.0),
            ListTile(
              title: Text('命名参数 <named parameters>'),
              onTap: () {
                void namedArgument(String name, {int age, String sex = 'maintain secrecy'}) {
                  print('name: $name, age: $age, sex: $sex');
                }

                namedArgument('Shawn Mendes');
                namedArgument('Shawn Mendes', sex: 'Female');
              },
            ),
            Divider(height: 1.0),
            ListTile(
              title: Text('匿名函数 <anonymous functions>'),
              onTap: () {
                final anuonymousFunc = () {
                  print('anonymousFunc');
                };
                anuonymousFunc();
              },
            ),
            Divider(height: 1.0),
            ListTile(
              title: Text('箭头函数 <arrow syntax>'),
              onTap: () {
                final List<String> fruits = ['apple', 'orange', 'banana'];
                fruits.forEach((value) {
                  print(value);
                });

                // or
                fruits.forEach((value) => print(value));

                // or
                final List<int> numbers = [3, 54, 23, 8, 49, 0, 4];
                final multipliedNumbers = numbers.map((value) {
                  return value * 3;
                });
                print(multipliedNumbers); // (9, 162, 69, 24, 147, 0, 12)
                print(multipliedNumbers.toList()); // [9, 162, 69, 24, 147, 0, 12]
              },
            ),
            Divider(height: 1.0),
            ListTile(
              title: Text('自执行方法'),
              onTap: () {
                (() {
                  print('Self executing method, e.g.1');
                })();

                // or
                ((num) {
                  print('Self executing method, num = $num, e.g.2');
                })(18);
              },
            ),
            Divider(height: 1.0),
          ],
        ),
      ),
    );
  }
}
